package org.se.scut.hibernate.dao;

import java.util.List;

import org.hibernate.Session;
import org.se.scut.hibernate.dao.model.Ablum;
import org.se.scut.hibernate.dao.model.Picture;

public class AblumDAO extends BaseDAO<Ablum>{

	public Ablum getAblumByLazy(int ablumID)
	{
		Session session = HibernateSessionFactory.getSessionFactory()
				.openSession();
		try {
			session.beginTransaction();
			Ablum owner = (Ablum) session.get(Ablum.class, ablumID);
			owner.getPictures();
			return owner;
		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Picture> getPicturesByCount(int ablumID, int from, int count)
	{
		String sql = "select abl from Ablum abl where abl.id = " + ablumID;
		sql += " order by abl.numOfCollected desc";
		Session session = HibernateSessionFactory.getSessionFactory()
				.openSession();
		try {
			session.beginTransaction();
			List<Picture> pictures = session.createQuery(sql).setFirstResult(from)
			.setMaxResults(count).list();
			for (Picture picture : pictures) {
				picture.getComments();
			}
			return pictures;
		}finally{
			session.getTransaction().commit();
			session.close();
		}
	}
	
}
